package jmind.dolphin.redis.server;

import jmind.dolphin.frame.DolphinCommand;
import jmind.dolphin.frame.Service;
import jmind.dolphin.frame.ServiceFactory;
import jmind.dolphin.redis.*;
import jmind.dolphin.util.DophinUtil;

public class DolphinRedisServer extends  AbstractDolphinRedisServer {

    private  final ServiceFactory serviceFactory = DophinUtil.getServiceFactory();
    @Override
    public BulkReply get(byte[] key0) throws RedisException {
        DolphinCommand command = new DolphinCommand(key0,null);
        String s= serviceFactory.getService(command.getServiceName()).get(command);
        return new BulkReply(s.getBytes());
    }

    @Override
    public Reply set(byte[] key0, byte[] value1) throws RedisException {
        DolphinCommand command = new DolphinCommand(key0,value1);
        String s= serviceFactory.getService(command.getServiceName()).set(command);
        return new BulkReply(s.getBytes());
    }

    @Override
    public IntegerReply del(byte[][] key0) throws RedisException {
        int r=0;
        for(int i=0;i<key0.length;i++){
            DolphinCommand command = new DolphinCommand(key0[i],null);
            r += serviceFactory.getService(command.getServiceName()).del(command);
        }
        return new IntegerReply(r);
    }

    @Override
    public IntegerReply exists(byte[] key0) throws RedisException {
        DolphinCommand command = new DolphinCommand(key0,null);
        Service service = serviceFactory.getService(command.getServiceName());
        if (service == null) {
            return new IntegerReply(1);
        } else {
            return new IntegerReply(0);

        }
    }
}
